#include "stdafx.h"
#include "coreinitializer.h"
#include "coresettings.h"

#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

namespace Core {

static
void initSDL()
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
    {
        std::cout << "SDL initialization error: " << SDL_GetError() << std::endl;
        exit(0);
    }

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
}

static
void initCoreSettings(bool needFullscreen)
{
    if (needFullscreen)
    {
        Core::CoreSettings::instance()->readSystemSettings();
    }
    else
    {
        Core::CoreSettings::instance()->setSettings(800, 600, false);
    }
}

static
void initOpenGL()
{
    glClearColor(0.0, 0.0, 0.0, 1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void CoreInitializer::init(bool needFullscreen)
{
    initSDL();
    initCoreSettings(needFullscreen);
    initOpenGL();
}

void CoreInitializer::destroy()
{
    SDL_Quit();
}

} // Core
